package priv.dajie.tzzq.spider.csgo.steam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import priv.dajie.tzzq.bot.BotManager;
import priv.dajie.tzzq.bot.steam.SteamUser;
import priv.dajie.tzzq.common.ApplicationContextUnit;
import priv.dajie.tzzq.service.SteamCsgoService;
import priv.dajie.tzzq.service.impl.SteamCsgoServiceImpl;
import priv.dajie.tzzq.spider.Base.Spider;
import priv.dajie.tzzq.spider.csgo.SpiderInfoElement;
import priv.dajie.tzzq.spider.csgo.SpiderInfos;
import priv.dajie.tzzq.spider.model.CsgoItemInfo;
import priv.dajie.tzzq.spider.model.CsgoItem;

import java.math.BigDecimal;
import java.net.URISyntaxException;

/**
 * @ClassName SteamCsgoSpider
 * @Description
 * @Author dajie
 * @Date 2019/11/8 11:29 下午
 * @Version V1.0
 **/
public class SteamCsgoSpider implements Spider, Runnable {


    private SteamCsgoService steamCsgoService = ApplicationContextUnit.getInstance().getBean(SteamCsgoServiceImpl.class);

    private SpiderInfos spiderInfos;
    private SteamUser steamUser = BotManager.getSteamBot().getSteamUser();




    public SteamCsgoSpider(SpiderInfos spiderInfos){
        this.spiderInfos = spiderInfos;
    }

    @Override
    public void run() {
        startSpider();
    }

    protected void getItem(SpiderInfoElement spiderInfoElement){
        int start = 0;
        int count = 100;
        int max=0;
        do {
            try {
                URIBuilder uriBuilder = new URIBuilder("https://steamcommunity.com/market/search/render/");
                uriBuilder.setParameter(spiderInfoElement.getKey(), spiderInfoElement.getValue());
                uriBuilder.setParameter("start", String.valueOf(start));
                uriBuilder.setParameter("count", String.valueOf(count));
                uriBuilder.setParameter("search_descriptions", "0");
                uriBuilder.setParameter("sort_column", "popular");
                uriBuilder.setParameter("norender", "1");
                uriBuilder.setParameter("appid", "730");
                String jsonStr = steamUser.open(new HttpGet(uriBuilder.build()), true);
                JSONObject json = JSON.parseObject(jsonStr);
                max = json.getInteger("total_count");
                JSONArray items = json.getJSONArray("results");
                for(int i=0;i<items.size();i++){
                    JSONObject item = items.getJSONObject(i);
                    CsgoItem csgoItem = new CsgoItem();
                    CsgoItemInfo csgoItemInfo = new CsgoItemInfo();
                    csgoItemInfo.setName(item.getString("name"));
                    csgoItemInfo.setSteamName(item.getString("hash_name"));;
                    String priceStr = item.getString("sell_price_text").replace("¥ ", "").replace(",", "");
                    csgoItemInfo.setType(item.getJSONObject("asset_description").getString("type"));
                    csgoItem.setNum(item.getInteger("sell_listings"));
                    csgoItem.setPrice(new BigDecimal(priceStr));
                    csgoItem.setCsgoItemInfo(csgoItemInfo);
                    steamCsgoService.addInfoAndItem(csgoItem);
                }

                start+=count;
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }


        }while (start<max);


    }

    @Override
    public void startSpider() {
        while (!spiderInfos.isEmpty()){
            SpiderInfoElement spiderInfoElement = spiderInfos.getSpiderInfoElement();
            getItem(spiderInfoElement);
            System.out.println("剩余待爬取个数:"+spiderInfos.getSize() + " | " + spiderInfoElement);
        }
    }

    public static void main(String[] args) {
        SpiderInfos spiderInfos = SteamCsgoInfo.getSpiderInfos();
        System.out.println("开始爬取");
        SteamCsgoSpider steamCsgoSpider = new SteamCsgoSpider(spiderInfos);
        steamCsgoSpider.run();

    }

}
